# coding:utf-8
# writingtime: 2022-8-3
# reference: https://doi.org/10.1007/s40815-021-01243-2

import numpy as np
from DistanceFunction.euclidean import Euclidean


class K:
    @staticmethod
    def getresult(dataList, membershipMatrix, clusterCenter, m=2, a=2):
        """
        function: k评价函数
        :param dataList: 样本向量
        :param membershipMatrix: 评价矩阵
        :param clusterCenter: 中心点向量
        :param m: 聚合方程参数
        :param m: 评价函数参数
        :return: k评价值
        """
        # 计算中心点的均值
        cluster_mean = np.array([0 for _ in range(len(clusterCenter[0]))])
        for i in clusterCenter:
            cluster_mean += np.array(i)
        cluster_mean = list(cluster_mean / len(clusterCenter))
        # 第一部分的累加和
        sum1 = 0
        temp_matrix = []
        for i in range(len(clusterCenter)):
            li_temp = []
            for j in range(len(dataList)):
                temp = Euclidean.getresult(dataList[j], clusterCenter[i]) ** 2
                if i != j:
                    li_temp.append(temp)
                sum1 += (membershipMatrix[i][j] ** m) * temp
            temp_matrix.append(li_temp)
        # 第二部分的累加和
        sum2 = 0
        for i in range(len(clusterCenter)):
            sum2 += Euclidean.getresult(clusterCenter[i], cluster_mean) ** 2
        sum2 /= len(clusterCenter)
        # 最小值
        minvalue = min(min(i) for i in temp_matrix)
        result = (sum1 + sum2) / (len(dataList) * minvalue)
        return result
